package cn.bluech.lib.utils;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Description:
 * UI适配规范：
 * xxxhdpi 2160x3840 640dpi 4.0 48dp->192px(图标大小)
 * xxhdpi  1080x1920 480dpi 3.0 48dp->144px
 * xhdpi    720x1080 320dpi 2.0 48dp->96px
 * hdpi     480x800  160dpi 1.5 48dp->72px
 * mdpi     320x800  160dpi 1.0 48dp->48px
 */
public class DispUtils {
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 测量字符高度
     */
    public static int getTextHeight(TextPaint textPaint, String text) {
        Rect bounds = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), bounds);
        return bounds.height();
    }

    /**
     * 测量字符宽度
     */
    public static int getTextWidth(TextPaint textPaint, String text) {
        return (int) textPaint.measureText(text);
    }


    public static String getScreenInfo(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if(null==windowManager)
            return "";

        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);

        return "resolution : " + metrics.heightPixels + 'x' + metrics.widthPixels + '\n' +
               "densityDpi : " + metrics.densityDpi + '\n' +      // 屏幕密度-每英寸点数
               "density    : " + metrics.density + '\n' +         // 显示器的逻辑密度
               "scaledDensity : " + metrics.scaledDensity + '\n'+ // 字体缩放系数
               "screenWidth : " + metrics.widthPixels/metrics.density + "dp\n"; // 屏幕宽度(dp)
    }

    public static String getRealScreenInfo(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if(null==windowManager)
            return "";

        DisplayMetrics metrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            windowManager.getDefaultDisplay().getRealMetrics(metrics);
        }else
            windowManager.getDefaultDisplay().getMetrics(metrics);

        return "resolution : " + metrics.heightPixels + 'x' + metrics.widthPixels + '\n' +
                "densityDpi : " + metrics.densityDpi + '\n' +      // 屏幕密度-每英寸点数
                "density    : " + metrics.density + '\n' +         // 显示器的逻辑密度
                "scaledDensity : " + metrics.scaledDensity + '\n'+ // 字体缩放系数
                "screenWidth : " + metrics.widthPixels/metrics.density + "dp\n"; // 屏幕宽度(dp)
    }
}
